package controller

import (
	"a-common/myerr"
	"a-system/domain/entity"
	"a-system/service"
	"fmt"
	"sync"

	"github.com/gofiber/fiber/v2"
)

var (
	sysMenuControllerTemp *SysMenuController
	sysMenuService        = service.NewSysMenuService()
	sysMenuControllerOnce sync.Once
)

type SysMenuController struct{}

func NewSysMenuController() *SysMenuController {
	sysMenuControllerOnce.Do(func() {
		sysMenuControllerTemp = &SysMenuController{}
	})
	return sysMenuControllerTemp
}

type SearchArgs struct {
	IsTree      bool   `json:"isTree"`
	Uuid        string `json:"uuid"`
	Page        int    `json:"page"`
	InitKey     string `json:"initKey"`
	Select      bool   `json:"select"`
	QuickSearch string `json:"quickSearch"`
}

// List 列表
func (m *SysMenuController) List(c *fiber.Ctx) error {
	var args SearchArgs
	if err := c.QueryParser(&args); err != nil {
		return myerr.ERR_SYS_ARGS
	}
	user := GetUser(c)

	if args.IsTree && args.Select {
		rows, err := sysMenuService.ListSelect(user.Uid)
		if err != nil {
			return err
		}
		return Resp(c, fiber.Map{
			"options": rows,
		})
	}

	rows, err := sysMenuService.Tree(user.Uid)
	if err != nil {
		return err
	}
	return Resp(c, fiber.Map{
		"list":   rows,
		"remark": "",
	})
}

// Get 查
func (m *SysMenuController) Get(c *fiber.Ctx) error {
	id := c.Query("id")
	if id == "" {
		return myerr.ERR_SYS_ARGS
	}
	row, err := sysMenuService.Get(id)
	if err != nil {
		return err
	}
	return Resp(c, fiber.Map{
		"row": row,
	})
}

// Add 增
func (m *SysMenuController) Add(c *fiber.Ctx) error {
	var args entity.SysMenuDto
	if err := c.BodyParser(&args); err != nil {
		return myerr.ERR_SYS_ARGS
	}
	user := GetUser(c)
	args.CreatedBy = user.Account

	_, err := sysMenuService.Add(&args)
	if err != nil {
		return err
	}
	return Ok(c)
}

// Edit 改
func (m *SysMenuController) Edit(c *fiber.Ctx) error {
	var args entity.SysMenuDto
	// 绑定参数上下文并校验参数
	if err := c.BodyParser(&args); err != nil {
		fmt.Println("err", err.Error())
		return myerr.ERR_SYS_ARGS
	}
	user := GetUser(c)
	args.UpdatedBy = user.Account

	_, err := sysMenuService.Edit(&args)
	if err != nil {
		return err
	}
	return Ok(c)
}

// Del 删
func (m *SysMenuController) Del(c *fiber.Ctx) error {
	ids := c.Params("ids")
	if ids == "" {
		return myerr.ERR_SYS_ARGS
	}
	_, err := sysMenuService.Del(ids)
	if err != nil {
		return err
	}
	return Ok(c)
}

// RoleMenuTreeSelect 通过角色权限获取菜单树
func (m *SysMenuController) RoleMenuTreeSelect(c *fiber.Ctx) error {
	roleId := c.Params("id")
	if roleId == "" {
		return myerr.ERR_SYS_ARGS
	}
	user := GetUser(c)

	mp, err := sysMenuService.RoleMenuTreeselect(roleId, user.Uid)
	if err != nil {
		return err
	}
	return Resp(c, mp)
}

// GetTree 获取菜单树
func (m *SysMenuController) GetTree(c *fiber.Ctx) error {
	var args entity.SysMenuDto
	if err := c.QueryParser(&args); err != nil {
		return myerr.ERR_SYS_ARGS
	}
	user := GetUser(c)

	mp, err := sysMenuService.GetTree(&args, user.Uid)
	if err != nil {
		return err
	}
	return Resp(c, mp)
}

// GetTreeSelect 获取菜单列表及根据权限id获取菜单id
func (m *SysMenuController) GetTreeSelect(c *fiber.Ctx) error {
	ids := c.Query("ids")
	if ids == "" {
		return myerr.ERR_SYS_ARGS
	}
	// 获取权限所拥有的菜单列表id
	mp, err := sysMenuService.GetTreeSelect(ids)
	if err != nil {
		return err
	}
	return Resp(c, mp)
}
